require "etc"

class BankAccount
  attr_reader :balance
  
  def initialize(starting_balance)
    @balance = starting_balance
  end
  
  def deposit(amount)
    @balance += amount
  end
  
  def withdraw(amount)
    @balance -= amount
  end
end

class AccountProtectionProxy
  def initialize(real_object, owner_name)
    @real_object, @owner_name = real_object, owner_name
  end
  
  def method_missing(name, *args)
    check_access
    @real_object.send(name, *args)
  end
  
  def check_access
    if Etc.getlogin != @owner_name
      raise "Illegal access: #{Etc.getlogin} cannot access account!"
    end
  end
end

class VirtualAccountProxy
  def initialize(&creation_block)
    @creation_block = creation_block
  end
  
  def subject
    @subject = @creation_block.call unless @subject
    @subject
  end
  
  def method_missing(name, *args)
    s = subject
    s.send(name, *args)
  end
end

